package funcs

import (
	"fmt"
	"github.com/toolkits/core"
	"github.com/toolkits/nux"
)
type Disk struct {
	FsSpec            string `json:"fs_spec"`    			//分区
	FsVfstype         string `json:"fs_vfstype"` 			//文件系统一般是xfs和ext4
	BlocksAll         string `json:"blocks_all"` 			//总的块大小
	BlocksUsed        string `json:"blocks_used"`			//已使用
	BlocksFree        string `json:"blocks_free"`			//空闲
	BlocksUsedPercent string `json:"blocks_used_percent"`	//使用比例
	BlocksFreePercent string `json:"blocks_free_percent"`	//空闲比例
}

func DiskInfo() *[]Disk{
	var diskList = []Disk{}
	mountPoints, err := nux.ListMountPoint()
	for idx := range mountPoints {
		var du *nux.DeviceUsage
		var disk Disk
		du, err = nux.BuildDeviceUsage(mountPoints[idx][0], mountPoints[idx][1], mountPoints[idx][2])
		if err == nil {
			disk.FsSpec = du.FsSpec
			disk.FsVfstype = du.FsVfstype
			disk.BlocksAll = core.ReadableSize(float64(du.BlocksAll))
			disk.BlocksUsed = core.ReadableSize(float64(du.BlocksUsed))
			disk.BlocksFree = core.ReadableSize(float64(du.BlocksFree))
			disk.BlocksUsedPercent = fmt.Sprintf("%.1f%%", du.BlocksUsedPercent)
			disk.BlocksFreePercent = fmt.Sprintf("%.1f%%", du.BlocksFreePercent)
		}
		diskList = append(diskList, disk)
	}
	return &diskList
}
